/**
 * Created by shbs on 04/07/2017.
 */
import Vue from "vue";
import Vuex from "vuex";
import actions from "./actions";
import * as types from "./mutation-types";
import merge from "../until/merge"
Vue.use(Vuex);
const state = {
    user:null,//登录用户
    goodslist:[],//商品列表,
    cartlist:[],
    initgoodpage:true,
    initcartpage:true,
    defadress:null,
    storeinfo:null,
    initstorepage:false
};
const getters = {};
const mutations = {
    [types.SET_USER](state, data){
        state.user=data;
    },
    //个人信息
    [types.SET_GOODS](state, data){
        state.goodslist=data;
    },
    [types.ADD_GOODS](state, data){
        state.goodslist=state.goodslist.concat(data);
    },


    [types.CHANGE_INITGOODS_PAGE](state, data){
        state.initgoodpage=data
    },
    [types.CHANGE_INITCART](state, data){
        state.initcartpage=data
    },
//个人中心 店铺页面
    [types.CHANGE_INITSTORE_PAGE](state, data){
        state.initstorepage=data
    },
    [types.SET_STORE](state, data){
        state.storeinfo=data
    },
//购物车
    [types.CHANGE_CARTITEM](state, {index,obj}){
        var item =state.cartlist[index];
        item=merge({},item,obj);
        state.cartlist[index]=item;
    },
    [types.REMMOVE_CART](state, index){
        state.cartlist.splice(index,1)
    },
    [types.SET_CART](state, data){
        state.cartlist=data;
    },
    [types.ADD_CART](state, data){
        state.cartlist=state.cartlist.concat(data);
    },
    //默认地址
    [types.SET_ADRESS](state,data){
        state.user.userAddress=data;
        localStorage.setItem("user",JSON.stringify( state.user));
    }
};
export default new Vuex.Store({
    state,
    actions,
    mutations,
    getters,
    strict: process.env.NODE_ENV !== 'production' //发布环境不使用严格模式
})